{extend name="public/base"}

{block name="title"}
编辑LOGO / 头像
{/block}

{block name="style"}
{css href="/static/wechat/css/index.css" /}
{css href="/static/wechat/popo/css/shop.css" /}
{/block}

{block name="body"}
<form action="?" id="form">
    <div class="pageControl1 startShop bg-f8">
        <div class="nav">
            <div class="msui_navTop">
                <a class="msui_navTop-back" href="javascript:history.back();"><i class="micon-back_fff"></i></a>
                <h1 class="msui_navTop-title" style="font-size: .8rem;;">编辑LOGO / 头像</h1>
                <a href="javascript:;" class="msui_navTop-login" id="s_btn_submit" style="color: #fff;">保存</a>
            </div>
        </div>
        <div class="prove active">
            <div class="weui-cell bg-ff" style=" border-bottom: 1px solid #eee; border-top: 1px solid #eee;">
                <p class="weui-label cl-3333" style="width:4.6rem;">logo/头像</p>
                <a href="#" class="list_img weui-cell__bd">
                    {notempty name="image"}
                        <img class="uploaderInput0_img" src="{$image}"  style="width:auto; margin-left: 0;" alt="" />
                    {else /}
                        <img class="uploaderInput0_img" src="/static/wechat/images/shop_logo_03.jpg" style="width:auto; margin-left: 0;" alt=""/>
                    {/notempty}
                    <input id="uploaderInput0" class="weui-uploader__input" type="file" accept="image/*"  file-attr="" />
                    <input name="image" type="hidden" value="{$image|default=''}" />
                    <input name="model" type="hidden" value="{$model|default=''}" />
                    <input name="field" type="hidden" value="{$field|default=''}" />
                    <input name="type" type="hidden" value="{$type|default=''}" />
                </a>
            </div>
        </div>
    </div>
</form>
<!-- loading toast -->
<div id="loadingToast" style="display:none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast" style="opacity: 1; visibility: visible;">
        <i class="weui-loading weui-icon_toast"></i>
        <p class="weui-toast__content">加载中...</p>
    </div>
</div>

{/block}
{block name="javascript"}
<script>
    $(function () {
        $("#uploaderInput0").change(function(){
            $('#loadingToast').show();
            window.setTimeout(function(){
                var formData = new FormData();
                formData.append("type","image");
                formData.append("img",$("#uploaderInput0")[0].files[0]);
                $.ajax({
                    url: '{:url("Base/get_upload_img")}' ,
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: true,
                    contentType: false,
                    processData: false,
                    dataType:"json",
                    success: function (d) {
                        $('#loadingToast').fadeOut();
                        if(!d.code){
                            var code = msg.code;
                            $.alert(error_code[code]);
                        }else{
                            $("input[name='image']").val(d.data.path);
                            $(".uploaderInput0_img").attr('src', SCOPE.image_host_url+d.data.path);
                        }
                    },
                    error: function (d) {
                        alert('网络异常');$('#loadingToast').fadeOut();
                    }
                });
            },666);

        });

        //  提交申请
        $("#s_btn_submit").click(function (e) {
            $('#loadingToast').fadeIn();
            $('.weui-toast__content').html('正在提交...');
            e.preventDefault();

            var _form = $("#form");
            $.ajax({
                type : 'POST',
                url : _form.attr('action'),
                data : _form.serialize(),
                dataType : 'json',
                success : function (msg) {
                    $('#loadingToast').fadeOut();
                    console.log(msg);
                    if (msg.code == 200) {
                        alert('编辑成功')
                        window.location.href=history.back();
                    } else {
                        var code = msg.code;
                        $.alert(error_code[code]);
                    }
                }
            });
        });
    });

</script>
{/block}